package com.tz.service.impl;

import com.tz.entity.*;
import com.tz.mapper.QuestionCategoryMapper;
import com.tz.mapper.ResearchAdviceMapper;
import com.tz.mapper.ResearchQuestionMapper;
import com.tz.mapper.ResearchTemplateMapper;
import com.tz.service.ResearchTemplateService;
import com.tz.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by 侯畅畅 on 2018/1/3.
 */
@Service
@Transactional
public class ResearchTemplateServiceImpl implements ResearchTemplateService {
    @Autowired
    ResearchTemplateMapper researchTemplateMapper;
    @Autowired
    ResearchAdviceMapper researchAdviceMapper;
    @Autowired
    ResearchQuestionMapper researchQuestionMapper;
    @Autowired
    QuestionCategoryMapper questionCategoryMapper;

    @Override
    public void addTemplate(ResearchTemplate researchTemplate) {
        //插入模板
        researchTemplateMapper.insertTemplate(researchTemplate);
        researchTemplate.setTempid(researchTemplate.getTempid());

        //级联插入问题类目
        List<QuestionCategory> questionCategorys = researchTemplate.getQuestioncategories();
        for (QuestionCategory q : questionCategorys) {
            q.setResearchTemplate(researchTemplate);
            questionCategoryMapper.insertQuestionCategory(q);
            q.setCateid(q.getCateid());
            //级联插入问题
            List<ResearchQuestion> researchquestions = q.getResearchquestions();
            for (ResearchQuestion r : researchquestions) {
                r.setQuestionCategory(q);
                researchQuestionMapper.insertResearchQuestion(r);
            }
        }


        //级联插入建议
        List<ResearchAdvice> researchAdvices = researchTemplate.getResearchadvices();
        for (ResearchAdvice researchAdvice : researchAdvices) {
            researchAdvice.setResearchTemplate(researchTemplate);
            researchAdviceMapper.insertResearchAdvice(researchAdvice);
        }


    }


    @Override
    public void alterTemplate(ResearchTemplate researchTemplate) {
        researchTemplateMapper.updateTemplate(researchTemplate);
        //级联删除问题
        List<QuestionCategory> questionCategories = questionCategoryMapper.selectQuestionCategorysByTempid(researchTemplate.getTempid());
        //遍历
        for (QuestionCategory questionCategory : questionCategories) {

            researchQuestionMapper.deleteResearchQuestion(questionCategory.getCateid());
        }
        //级联删除类目
        questionCategoryMapper.deleteQuestionCategory(researchTemplate.getTempid());
        //级联删除建议
        researchAdviceMapper.deleteResearchAdvice(researchTemplate.getTempid());


        //级联插入问题类目
        List<QuestionCategory> questionCategorys = researchTemplate.getQuestioncategories();
        for (QuestionCategory q : questionCategorys) {
            q.setResearchTemplate(researchTemplate);
            questionCategoryMapper.insertQuestionCategory(q);
            q.setCateid(q.getCateid());
            //级联插入问题
            List<ResearchQuestion> researchquestions = q.getResearchquestions();
            for (ResearchQuestion r : researchquestions) {
                r.setQuestionCategory(q);
                researchQuestionMapper.insertResearchQuestion(r);
            }
        }
        //级联插入建议
        List<ResearchAdvice> researchAdvices = researchTemplate.getResearchadvices();
        for (ResearchAdvice researchAdvice : researchAdvices) {
            researchAdvice.setResearchTemplate(researchTemplate);
            researchAdviceMapper.insertResearchAdvice(researchAdvice);
        }
    }

    @Override
    public void removeTemplate(Long id) {
        //级联删除问题
        List<QuestionCategory> questionCategories = questionCategoryMapper.selectQuestionCategorysByTempid(id);
        //遍历
        for (QuestionCategory questionCategory : questionCategories) {
            researchQuestionMapper.deleteResearchQuestion(questionCategory.getCateid());
        }
        //级联删除类目
        questionCategoryMapper.deleteQuestionCategory(id);
        //级联删除建议
        researchAdviceMapper.deleteResearchAdvice(id);
        //删除模板
        researchTemplateMapper.deleteTemplate(id);
    }

    @Override
    public PageBean<ResearchTemplate> findResearchTemplateByPage(Integer pageNow, Integer pageSize) {
        PageBean<ResearchTemplate> pageBean = new PageBean<>();
        pageBean.setPageNow(pageNow);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalRecords(researchTemplateMapper.countResearchTemplate());
        pageBean.setPageDatas(researchTemplateMapper.selectResearchTemplateByPage(pageNow, pageSize));
        //返回数据
        return pageBean;
    }

    @Override
    public ResearchTemplate getResearchTemplate(Long id) {
        ResearchTemplate researchTemplate = researchTemplateMapper.selectResearchTemplateById(id);
        researchTemplate.setQuestioncategories(questionCategoryMapper.selectQuestionCategorysByTempid(researchTemplate.getTempid()));
        researchTemplate.setResearchadvices(researchAdviceMapper.selectResearchAdviceByTempid(researchTemplate.getTempid()));
        return researchTemplate;
    }

    @Override
    public Integer countUseTime(Long tempid) {
        return researchTemplateMapper.countTemplateUseTime(tempid);
    }

    @Override
    public List<ResearchTemplate> getAllResearchTemplate(Integer status) {
        return researchTemplateMapper.getAllResearchTemplate(status);
    }
}